package com.net.core.models.user.service;

import com.net.core.core.sql.YxSqlPlus;
import com.net.core.models.BaseDao;
import com.net.core.models.bean.Role;
import com.net.core.models.bean.User;
import com.net.core.core.YXException;
import com.net.core.utils.MyUtils;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserService extends BaseDao {

    static Map<String, User> userMap = new HashMap();

    public User getUserByAccount(String token) {
        return null;
    }

    private static ThreadLocal<String> currentToken = new ThreadLocal<>();

    public static void setToken(String token) {
        currentToken.set(token);
    }

    public static String getToken() {
        return currentToken.get();
    }

    public static void setUser(User user) {
        String token = user.getAccount();
        if (token == null || token.trim().length() == 0) throw new YXException("token 生成有误");
        user.setToken(token);
        userMap.put(token, user);
    }

    public static User getUser(String token) {
        return null;
    }

    public List<User> getUser(String key, User user) {
        String roleId = user.getRoleName();
        List<String> roles = Role.getGmRoleName(roleId);
        YxSqlPlus<User> sqlPlus = new YxSqlPlus(User.class);
        sqlPlus.select().in(User::getRoleName, roles);
        if (key != null && key.trim().length() > 0) {
            sqlPlus.orStart().like(User::getName, key).like(User::getPinyin, key).orEnd();
        }
        sqlPlus.orderBy(User::getId);
        return selectList(sqlPlus, User.class);
    }

    public User login(String username, String password) {
        User user = new User();
        user.setAccount(username);
        user.setPassword(password);
        user.setToken("-000000");
        return user;
    }
}
